package com.CTAtracker.android.Fragments;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.Activities.LineActivity;
import com.CTAtracker.android.Activities.StationActivity;
import com.CTAtracker.android.Adapters.StationsSearchItemAdapter;
import com.CTAtracker.android.DAO.StationDataSource;
import com.CTAtracker.android.Models.ColorStation;
import com.CTAtracker.android.Models.Station;

/**
 * Fragment regarding the search of a station in a specific line
 */
public class SearchStationFragment extends Fragment {
	/**
	 * The Id of the specific line 
	 */
	private int _idLine;
	/**
	 * The list of station in the specific line
	 */
	private ArrayList<Station> _listStation;
	/**
	 * The view of the list of station
	 */
	private ListView _listViewStation;
	/**
	 * The attribute handling the connection to the database
	 */
	private StationDataSource _stationDataSource;
	/**
	 * Listener on the list view. Retrieve the Station selected and display the new Activity
	 */
	private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position, long id) {

			Intent intent = new Intent((LineActivity)getActivity(), StationActivity.class);
			intent.putExtra("idStation", _listStation.get(position).getStationId());
			intent.putExtra("lineId", _listStation.get(position).get_line_id());
			startActivity(intent);
		}
	};

	/**
	 * Default constructors
	 */
	public SearchStationFragment(){
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		_stationDataSource = new StationDataSource(container.getContext());
		return inflater.inflate(R.layout.searchstation, container, false);
	}

	@Override
	public void onStart() {
		super.onStart();

		LinearLayout linearLayout = (LinearLayout) getView().findViewById(R.id.layoutSearchStation);
		
		ColorStation color = new ColorStation(_idLine-1);
		linearLayout.setBackgroundColor(Color.parseColor(color.toString()));

		TextView textView = (TextView) getView().findViewById(R.id.SearchStation);
		textView.setText(color.getStationName());

		_stationDataSource.open();
		_listStation = (ArrayList<Station>) _stationDataSource.getStation(_idLine); 
		_stationDataSource.close();

		_listViewStation = (ListView) getView().findViewById(R.id.listViewSearchStation);
		StationsSearchItemAdapter adapter = new StationsSearchItemAdapter(getActivity(), R.layout.search_station_row, _listStation);
		_listViewStation.setAdapter(adapter);

		_listViewStation.setOnItemClickListener(mMessageClickedHandler);
	}

	/**
	 * Set the new idLine
	 * @param _idLine the new idLine
	 */
	public void setIdLine(int idLine) {
		_idLine = idLine;
	}
}
